/*
 * Automatic playlist by artist generator.
 * Copyright (C) 2020 positiv_

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <gtk/gtk.h>
#include <gmodule.h>

#include <deadbeef/deadbeef.h>
#include <deadbeef/gtkui_api.h>

static ddb_gtkui_t *gtkui_plugin;
static DB_functions_t *deadbeef;

static GtkWidget *filechooser;
static GSList *file_paths_in_selected_folder;

static int
auto_playlist_by_artist_generator_start()
{
    return 0;
}

static int
auto_playlist_by_artist_generator_stop()
{
    return 0;
}

static int
auto_playlist_by_artist_generator_connect()
{
	gtkui_plugin = (ddb_gtkui_t *)deadbeef->plug_get_for_id(DDB_GTKUI_PLUGIN_ID);

	if(!gtkui_plugin) {
		return -1;
	}

	return 0;
}

static void
add_files_from_glist(gpointer data, gpointer user_data)
{
	char *folder = strrchr((char*)data, G_DIR_SEPARATOR);

	int current_plt = deadbeef->plt_add(deadbeef->plt_get_count(), folder + 1);
	deadbeef->plt_set_curr_idx(current_plt);
	deadbeef->log("auto_playlist_by_artist_generator: Adding folder \"%s\".\n", data);

	deadbeef->plt_add_dir2(-1, deadbeef->plt_get_for_idx(current_plt), data, NULL, NULL);
	deadbeef->pl_save_current();
}

static int
auto_playlist_by_artist_generator_callback(DB_plugin_action_t *action, int ctx)
{
	filechooser = gtk_file_chooser_dialog_new("Choose folders", GTK_WINDOW (gtkui_plugin->get_mainwin()), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_STOCK_OK, GTK_RESPONSE_OK, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);

	gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(filechooser), TRUE);
	gtk_widget_show_all(filechooser);

	gint resp = gtk_dialog_run(GTK_DIALOG(filechooser));

	if (resp == GTK_RESPONSE_OK) {
		file_paths_in_selected_folder = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(filechooser));

		g_slist_foreach(file_paths_in_selected_folder, add_files_from_glist, NULL);
		g_slist_free(file_paths_in_selected_folder);
	}

	gtk_widget_destroy(filechooser);

	return 0;
}

static DB_plugin_action_t auto_playlist_by_artist_generator_action = {
    .title = "File/Import folder(s) as playlist(s)",
    .name = "auto_playlist_by_artist_generator",
    .flags = DB_ACTION_COMMON | DB_ACTION_ADD_MENU,
    .callback2 = auto_playlist_by_artist_generator_callback,
    .next = NULL,
};

static DB_plugin_action_t *
auto_playlist_by_artist_generator_getactions(DB_playItem_t *it)
{
    return &auto_playlist_by_artist_generator_action;
}

static DB_misc_t plugin = {
	.plugin.api_vmajor = DB_API_VERSION_MAJOR,
	.plugin.api_vminor = 0,
	.plugin.type = DB_PLUGIN_MISC,
	.plugin.version_major = 0,
	.plugin.version_minor = 1,
	.plugin.id = "apg",
	.plugin.name = "Automatic playlist by artist generator",
	.plugin.descr = "Generate playlists for your artists, by selecting the respective directories.",
	.plugin.copyright = "Copyright (C) 2020 positiv_\n"
        "\n"
        "This program is free software; you can redistribute it and/or\n"
        "modify it under the terms of the GNU General Public License\n"
        "as published by the Free Software Foundation; either version 2\n"
        "of the License, or (at your option) any later version.\n"
        "\n"
        "This program is distributed in the hope that it will be useful,\n"
        "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
        "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
        "GNU General Public License for more details.\n"
        "\n"
        "You should have received a copy of the GNU General Public License\n"
        "along with this program; if not, write to the Free Software\n"
        "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n",
	.plugin.website = "https://notabug.org/positiv_/Automatic-playlist-by-artist-generator",
	.plugin.start = auto_playlist_by_artist_generator_start,
	.plugin.stop = auto_playlist_by_artist_generator_stop,
	.plugin.get_actions = auto_playlist_by_artist_generator_getactions,
	.plugin.connect = auto_playlist_by_artist_generator_connect,
};

DB_plugin_t *
auto_playlist_by_artist_generator_load(DB_functions_t *ddb)
{
    deadbeef = ddb;
    return DB_PLUGIN(&plugin);
}
